package me.zingon.controller;

import com.github.pagehelper.PageInfo;
import me.zingon.model.Material;
import me.zingon.model.User;
import me.zingon.service.MaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/materials")
public class MaterialsController {


    @Autowired
    MaterialsService materialsService;

    @RequestMapping("/materials.*")
    public String upload(@RequestParam("xq")String xq,
                      @RequestParam("describe")String describe,
                      @RequestParam("subject")String subject,
                      @RequestParam("teacher")String teacher,
                      @RequestParam("materials")MultipartFile[] materials,
                      @RequestParam(required = false,defaultValue = "0")int offline,
                      HttpServletRequest req) throws IOException {
        Material m=new Material();
        m.setDeletedFlag(0);
        m.setDescribe(describe);
        m.setSubject(subject);
        m.setTeacher(teacher);
        m.setXq(xq);
        m.setOffline(offline==1);
        m.setCreateDate(new Timestamp(new Date().getTime()));
        User u=(User)req.getSession().getAttribute("user");
        if(u==null)
            m.setUId(-1);
        else
            m.setUId(u.getId());
        materialsService.save(m,materials);
        return "redirect:/materials.pj"+(req.getSession().getAttribute("style")==null?".pj":req.getSession().getAttribute("style"));
    }

    @RequestMapping("/search.ztc")
    @ResponseBody
    public Object getMaterials(@RequestParam(required = false,defaultValue = "")String xq,
                               @RequestParam(required = false,defaultValue = "")String subject,
                               @RequestParam(required = false,defaultValue = "")String teacher,
                               @RequestParam(required = false,defaultValue = "-1")int offline,
                               @RequestParam(required = false,defaultValue = "-1")int star,
                               @RequestParam(required = false,defaultValue = "1")int pageNumber,
                               @RequestParam(required = false,defaultValue = "10")int pageSize){
        PageInfo<Material> pageInfo = materialsService.getMaterialWithSth(xq,subject,teacher,offline,star,pageNumber,pageSize);
        Map date=new HashMap();
        date.put("total",pageInfo.getTotal());
        date.put("rows",pageInfo.getList());
        return date;
    }
}
